package org.tczs93.springsecurity.config.security;

import org.springframework.security.core.authority.AuthorityUtils;
import org.tczs93.springsecurity.config.exception.CustomerException;
import org.tczs93.springsecurity.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author ：tao.li
 * @description：用户登录校验
 * @date ：Created in 2021/8/28 23:31
 */
@Component
public class CustomerUserDetailsService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        org.tczs93.springsecurity.model.User user1 = userMapper.findByPhone(username);
        if(user1 == null) {
            throw new CustomerException("用户名或密码错误");
        }
        //这个权限可以写在表里面
        User user = new User(username,user1.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
        return user;
    }

}
